<template>
  <el-card shadow="never">
    <div slot="header" class="clearfix">
      <span>代码提交记录</span>
      <el-button style="float: right; padding: 3px 0" type="text" @click="openCommit()">更多</el-button>
    </div>
    <div style="height: 320px;overflow:auto;">
      <el-timeline>
        <el-timeline-item
          v-for="(item, index) in commitData"
          :key="index"
          :timestamp="item.timestamp">
          <el-link :underline="false" @click="openCommit(item.sha)">{{ item.content }}</el-link>
        </el-timeline-item>
      </el-timeline>
    </div>
  </el-card>
</template>

<script>
import {reposCommits} from "@/api/index/gitee";
import dayjs from "dayjs";
import Cookies from 'js-cookie'

export default {
  name: "Commit",
  data() {
    return {
      commitData: []
    }
  },
  created() {
    const commitData = Cookies.get('commitData');
    if (commitData) {
      this.commitData = JSON.parse(commitData)
    } else {
      this.getList()
    }
  },
  methods: {
    getList() {
      reposCommits({page: 1, per_page: 10}).then(res => {
        const commitData = []
        for (const item of res) {
          const commit = {
            sha: item.sha,
            content: item.commit.message,
            timestamp: dayjs(item.commit.committer.date).format('YYYY-MM-DD HH:mm:ss')
          }
          commitData.push(commit)
        }
        this.commitData = commitData
        Cookies.set('commitData', JSON.stringify(commitData), {
          // 半小时过期
          expires: new Date(new Date().getTime() + 0.5 * 60 * 60 * 1000)
        })
      })
    },
    openCommit(sha) {
      if (sha) {
        window.open('https://gitee.com/hellolyb/ruoyi-boot/commit/' + sha)
      } else {
        window.open('https://gitee.com/hellolyb/ruoyi-boot/commits/master')
      }
    }
  }
}
</script>

<style scoped lang="scss">

</style>
